package com.first.nell.extend

import android.annotation.SuppressLint
import androidx.core.text.isDigitsOnly
import com.first.nell.enums.CountryEnum
import com.first.nell.network.ApiUrl
import com.first.nell.utils.UsageCountryUtil
import java.io.File
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern

fun String.safeFirstNellString(): String {
    if (this.isBlank()) {
        return ""
    }
    return this
}

fun String.safeFirstNellInt(): Int {
    if (this.isBlank()) {
        return 0
    }
    if (this.isDigitsOnly()) {
        return this.toInt()
    }
    return 0
}

fun String.prefixFirstNellWithFirstNellAddFirstNellUnit(): String {
    return ("+$this")
}

fun String.getRealContent(): String {
    val splitStart = this.indexOf("#")
    if (splitStart >= 0) {
        val content = this.substring(splitStart + 1)
        return content
    }
    return this
}

fun String.prefixFirstNellWithFirstNellMoneyFirstNellUnit(): String {
    if (ApiUrl.BASE_PATH.contains(CountryEnum.NIGERIA.path, true)) {
        return (CountryEnum.NIGERIA.currency + " " + this.toFirstNellFinanceFirstNellString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.GHANA.path, true)) {
        return (CountryEnum.GHANA.currency + " " + this.toFirstNellFinanceFirstNellString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.KENYA.path, true)) {
        return (CountryEnum.KENYA.currency + " " + this.toFirstNellFinanceFirstNellString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.UGANDA.path, true)) {
        return (CountryEnum.UGANDA.currency + " " + this.toFirstNellFinanceFirstNellString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.COTEDIVA.path, true)) {
        return (CountryEnum.COTEDIVA.currency + " " + this.toFirstNellFinanceFirstNellString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.CAMEROON.path, true)) {
        return (CountryEnum.CAMEROON.currency + " " + this.toFirstNellFinanceFirstNellString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.VIETNAM.path, true)) {
        return (CountryEnum.VIETNAM.currency + " " + this.toFirstNellFinanceFirstNellString())
    }
    return (this.toFirstNellFinanceFirstNellString())
}

fun String.toFirstNellDaysFirstNellSuffix(): String {
    if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva() or UsageCountryUtil.isFirstNellCountryFirstNellXaf()) {
        return ("$this jours")
    }
    if (UsageCountryUtil.isFirstNellCountryFirstNellVietnam()) {
        return ("$this ngày")
    }
    return ("$this days")
}

fun String.toFirstNellFinanceFirstNellString(): String {
    if (this.isNotBlank()) {
        val resultDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
        return resultDecimal.toPlainString()
    }
    return this
}

fun String.toFirstNellFinanceFirstNellStringFirstNellWithFirstNellMoneyFirstNellFlag(): String {
    if (this.isNotBlank()) {
        val resultDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
        return resultDecimal.toPlainString()
    }
    return this
}

fun String.toFirstNellInterestFirstNellRateFirstNellString(): String {
    if (this.isNotBlank()) {
        try {
            val valueDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
            val edgeDecimal = BigDecimal(100)
            val resultDecimal: BigDecimal = valueDecimal.multiply(edgeDecimal)
            if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva() or UsageCountryUtil.isFirstNellCountryFirstNellXaf()) {
                return ("${resultDecimal.stripTrailingZeros().toPlainString()}% per jour")
            }
            if (UsageCountryUtil.isFirstNellCountryFirstNellVietnam()) {
                return ("${resultDecimal.stripTrailingZeros().toPlainString()}% per ngày")
            }
            return ("${resultDecimal.stripTrailingZeros().toPlainString()}% per day")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    return this
}

fun String.toFirstNellEncryptFirstNellPhoneFirstNellNumber(): String {
    if (this.isNotBlank()) {
        val firstLine = if (this.length >= 3) this.substring(0, 3) else this
        val lastLine = if (this.length >= 6) this.substring(this.length - 3) else ""
        return "$firstLine **** $lastLine"
    }
    return this
}

fun String.toFirstNellPhoneFirstNellNumberFirstNellInputFirstNellHint(): String {
    if (this.isNotBlank()) {
        if (this.length >= 4) {
            var tempString = ""
            if (tempString.isBlank()) {
                tempString = this.substring(0, 4)
            }

            if (this.length >= 7) {
                tempString += " "
                tempString += this.substring(4, 7)
            }

            if (this.length >= 10) {
                tempString += " "
                tempString += this.substring(7)
            }
            return tempString

        } else {
            return this
        }
    }
    return this
}

fun String.filterFirstNellForFirstNellPhoneFirstNellNumber(): String {
    val regEx = "[^0-9]"
    val p = Pattern.compile(regEx)
    val m = p.matcher(this)
    return m.replaceAll("").trim()
}

fun String.isFirstNellLink(): Boolean {
    return (this.safeFirstNellString().startsWith("http"))
}

fun String.isFirstNellEmailFirstNellFormat(): Boolean {
    if (this.safeFirstNellString().isNotBlank()) {
        return this.contains("@")
    }
    return false
}

fun String.suffixFirstNellPath(): String {
    try {
        val file = File(this)
        return file.name
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return this
}

@SuppressLint("SimpleDateFormat")
fun String.dateFirstNellForFirstNellFormatFirstNellString(targetFormat: String): Date? {
    try {
        val sDateFormat = SimpleDateFormat(targetFormat)
        val sdate = sDateFormat.parse(this)
        return sdate
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

fun String.toFirstNellWords(): String {
    if (this.isBlank()) {
        return ""
    }

    var tempString = ""
    val wordList = this.split("_")
    try {
        for (index in 0 until wordList.size) {
            val word = wordList[index]
            if (word.isNotBlank()) {
                val capi = word.substring(0, 1).uppercase()
                val last = word.substring(1).lowercase()
                if (tempString.isBlank()) {
                    tempString = (capi + last)
                } else {
                    tempString = tempString + " " + (capi + last)
                }
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

    return tempString
}
